package com.fengke.cn.yejian11.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.fengke.cn.yejian11.R;

import com.fengke.cn.yejian11.activity.DingDan1Activity;
import com.fengke.cn.yejian11.activity.DingDanActivity;
import com.fengke.cn.yejian11.config.Config;
import com.fengke.cn.yejian11.javabean.UserDingdan;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/10/23 0023.
 */

public class UserDingDanAdapter extends RecyclerView.Adapter {

    private List<UserDingdan> userDingdan;
    private Context context;
    private LayoutInflater layoutInflater;
    public UserDingDanAdapter(Context context,List<UserDingdan> userDingdan){
        this.context=context;
        this.userDingdan=userDingdan;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView=LayoutInflater.from(context).inflate(R.layout.item_user_dingdan,viewGroup,false);
        UserDingDanViewHolder holder=new UserDingDanViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        UserDingDanViewHolder userDingDanViewHolder= (UserDingDanViewHolder) viewHolder;
        final UserDingdan dingdan=userDingdan.get(position);
        String id=dingdan.getId();
        String state=dingdan.getVstate();
        String pic= Config.PIC_URL+dingdan.getThumb();
        String name=dingdan.getName();
        String price=dingdan.getPrice();
        String time=dingdan.getUpdate_time();
        String pay=dingdan.getPay();

        userDingDanViewHolder.dingdan_id.setText(id);
        if (dingdan.getVstate().equals("0")){
            userDingDanViewHolder.dingdan_state.setText("未参加");
        }else if (dingdan.getVstate().equals("1")){
            userDingDanViewHolder.dingdan_state.setText("已参加");
        }else if (dingdan.getVstate().equals("3")){
            userDingDanViewHolder.dingdan_state.setText("申请退款");
        }else {
            userDingDanViewHolder.dingdan_state.setText("已退款");
        }
        userDingDanViewHolder.dingdan_name.setText(name);
        userDingDanViewHolder.dingdan_price.setText(price);
        userDingDanViewHolder.dingdan_time.setText(time);
        userDingDanViewHolder.dingdan_pay.setText(pay);
        Picasso.with(context).load(pic).into(userDingDanViewHolder.dingdan_pic);

        if (dingdan.getType().equals("2")){
        userDingDanViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, DingDanActivity.class);
                intent.putExtra("DingDan_id",dingdan.getId());
                context.startActivity(intent);
            }
        });}
        else {
            userDingDanViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, DingDan1Activity.class);
                    intent.putExtra("DingDan_id",dingdan.getId());
                    context.startActivity(intent);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return userDingdan.size();
    }

    public class UserDingDanViewHolder extends RecyclerView.ViewHolder{

        public TextView dingdan_id;
        public TextView dingdan_state;
        public TextView dingdan_name;
        public TextView dingdan_price;
        public TextView dingdan_time;
        public TextView dingdan_pay;
        public ImageView dingdan_pic;
        public View itemView;

        public UserDingDanViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            dingdan_id= (TextView) itemView.findViewById(R.id.user_dingdan_id);
            dingdan_state= (TextView) itemView.findViewById(R.id.user_dingdan_state);
            dingdan_name= (TextView) itemView.findViewById(R.id.user_dingdan_name);
            dingdan_price= (TextView) itemView.findViewById(R.id.user_dingdan_price);
            dingdan_time= (TextView) itemView.findViewById(R.id.user_dingdan_time);
            dingdan_pay= (TextView) itemView.findViewById(R.id.user_dingdan_pay);
            dingdan_pic= (ImageView) itemView.findViewById(R.id.user_dingdan_pic);
        }
    }
}
